﻿using PoemGame.Management.Domain;
using PoemGame.Query;

namespace PoemGame.Management.Application
{
    /// <summary>
    /// 游戏管理的实现
    /// </summary>
    public class ManagementService:IManagementService
    {
        private readonly IGameRepository _gameRepository;
        private readonly IPlayerRepository _playerRepository;
        private readonly IGameQuery _gameQuery;
        private readonly IPlayerQuery _playerQuery;
        /// <summary>
        /// 依赖注入传入存储库实现和查询实现
        /// </summary>
        /// <param name="gameRepository"></param>
        /// <param name="playerRepository"></param>
        /// <param name="gameQuery"></param>
        /// <param name="playerQuery"></param>
        public ManagementService(IGameRepository gameRepository,
            IPlayerRepository playerRepository,
            IGameQuery gameQuery,
            IPlayerQuery playerQuery)
        {
            _gameRepository = gameRepository;
            _playerRepository = playerRepository;
            _gameQuery = gameQuery;
            _playerQuery = playerQuery;
        }
        
        public async Task BlockPlayer(Guid playerId)
        {
            var player=await _playerQuery.GetPlayerResAsync(playerId);
            if (player == null)
            {
                await _playerRepository.Add(new Player(playerId, true));
            }
            else
            {
                await _playerRepository.Update(new Player(playerId, true));
            }
        }

        public async Task UnBlockPlayer(Guid playerId)
        {
            var player = await _playerQuery.GetPlayerResAsync(playerId);
            if (player == null)
            {
                await _playerRepository.Add(new Player(playerId, false));
            }
            else
            {
                await _playerRepository.Update(new Player(playerId, false));
            }
        }

        public async Task MakeGameDeleted(Guid gameId)
        {
            var game = await _gameQuery.GetGameResAsync(gameId);
            if (game == null)
            {
                await _gameRepository.Add(new Game(gameId, true));
            }
            else
            {
                await _gameRepository.Update(new Game(gameId, true));
            }
        }

        public async Task RestoreDeletedGame(Guid gameId)
        {
            var game = await _gameQuery.GetGameResAsync(gameId);
            if (game == null)
            {
                await _gameRepository.Add(new Game(gameId, false));
            }
            else
            {
                await _gameRepository.Update(new Game(gameId, false));
            }
        }
    }
}
